package com.ma.workorder;

import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ma.R;

import java.lang.reflect.Field;

import base.BaseActivity;

public class ProcessedOrderInfoActivity extends BaseActivity {


    private SectionsPagerAdapter mSectionsPagerAdapter;
    private  static  int DIMEN_10_DP = 10;
    private ViewPager mViewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_processed_order_info);
        DIMEN_10_DP= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
        mSectionsPagerAdapter = new  SectionsPagerAdapter(getSupportFragmentManager());


        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);


        LinearLayout linearLayout = (LinearLayout) tabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setPadding(DIMEN_10_DP,DIMEN_10_DP,DIMEN_10_DP,DIMEN_10_DP);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this,
                R.drawable.layout_h_divider1px));
        setIndicator(this,tabLayout);


    }

    public static void setIndicator(Context context, TabLayout tabs  ) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout ll_tab = null;
        try {
            ll_tab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = DIMEN_10_DP;
        int right = DIMEN_10_DP;

        for (int i = 0; i < ll_tab.getChildCount(); i++) {
            View child = ll_tab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }


    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0: return FragmentOrderInfo.newInstance(position,"工单详清");

                case 1:return FragmentFeedback.newInstance(position,"完工反馈");

                case 2:return FragmentVistor.newInstance(position,"工单回访");

                case 3: return FragmentCustomerFeedback.newInstance(position,"客户反馈");

                default:break;

            }
            return null;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "工单详清";
                case 1:
                    return "完工反馈";
                case 2:
                    return "工单回访";
                case 3:
                    return "客户反馈";
            }
            return null;
        }
    }
}
